package com.woniuxy.movie.order.mq;

import com.woniuxy.movie.order.dao.OrderInfoMapper;
import com.woniuxy.movie.order.dao.OrderLogMapper;
import com.woniuxy.movie.order.exception.OrderException;
import com.woniuxy.movie.order.model.OrderInfo;
import com.woniuxy.movie.order.model.OrderLog;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class CreateLogConsumer {

    @Autowired
    private OrderLogMapper orderLogMapper;
    @Autowired
    private OrderInfoMapper orderInfoMapper;

    /**
     * 生成订单日志
     * @param orderLog
     * @throws Exception
     */
    @RabbitListener(queuesToDeclare = {@Queue(name = "create-orderLog")})
    @RabbitHandler
    public void consumer(OrderLog orderLog) throws Exception{
        OrderInfo orderInfo = orderInfoMapper.selectById(orderLog.getOrderId());
        if (orderInfo==null) {
            throw new OrderException(1007,"该订单不存在,日志生成失败");
        }
        orderLogMapper.insert(orderLog);
    }
}
